<template>
  <div class="app-container">
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" v-model="listQuery.title">
    </el-input>
    <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>


    <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">
      新建
    </el-button>


    <el-table :data="list" v-loading="listLoading" element-loading-text="Loading" border fit highlight-current-row>


      <el-table-column align="center" label='ID' width="95">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>


      <el-table-column label="用户名">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>

      <el-table-column label="年龄">
        <template slot-scope="scope">
          {{scope.row.age}}
        </template>
      </el-table-column>


      <el-table-column label="密码">
        <template slot-scope="scope">
          {{scope.row.password}}
        </template>
      </el-table-column>

      <el-table-column label="类型">
        <template slot-scope="scope">
          {{scope.row.type}}
        </template>
      </el-table-column>


      <el-table-column min-width="150px" label="操作">
        <template slot-scope="scope">
          <span class="link-type" @click="handleUpdate(scope.row)">编辑</span>
        </template>
      </el-table-column>


    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>


    <el-dialog :title="dialogStatus" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="70px"
               style='width: 400px; margin-left:50px;'>

        <el-form-item label="用户名" prop="name">
          <el-input v-model="temp.name"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="temp.password"></el-input>
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-input v-model="temp.age"></el-input>
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-input v-model="temp.type"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='新建'" type="primary" @click="createData">创建</el-button>
        <el-button v-if="dialogStatus=='修改'" type="primary" @click="updateData">修改</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {getUserList} from '@/api/user'
  import {createUser} from "../../api/user";
  import {updateUser} from "../../api/user";

  export default {
    name: "user",
    data() {
      return {
        tableKey: 0,
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
        temp: {
          id: undefined,
          importance: 1,
          remark: '',
          timestamp: new Date(),
          name: '',
          type: '',
          age: '',
          status: 'published'
        },
        dialogFormVisible: false,
        dialogStatus: '',
        rules: {
          name: [{required: true, message: 'name is required', trigger: 'blur'}]
        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        getUserList(this.listQuery).then(response => {

          this.list = response.data.items
          this.total = response.data.total;
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleCreate() {
        this.dialogStatus = '创建'
        this.dialogFormVisible = true
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = '修改'
        this.dialogFormVisible = true
      },


      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
            this.temp.author = 'vue-element-admin'

            createUser(this.temp).then(() => {
              this.list.unshift(this.temp)
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })

          }
        })
      },

      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)

            updateUser(tempData).then(() => {
              for (const v of this.list) {
                if (v.id === this.temp.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.temp)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
    },

  }


</script>

<style scoped>

</style>
